package XOrules;

import com.sun.org.apache.bcel.internal.generic.RETURN;

import java.util.*;
import java.lang.*;
import java.math.*;

public class XOGameManager extends XOrules.GameManager{

    public final static String GameName = "XO";

    public Object bgtp_mail;
    public String gameName;
    public XOrules.XOdata data;

    public XOGameManager(Object bgtp_mail_) {
		super();
        this.bgtp_mail = bgtp_mail_;
		this.name = USR_GAMEMANAGER;
		this.client = false;
		this.gameName = "XO";
		this.data = new XOdata.XOdata();
		super.setState(this, GM_SETTINGS);
    }
	
	//#add gamer
	public String pre_addGamer(XOrules.Request req){
		if (this.data.gamers.count(req.usr) != 0)
			return "this player here";
        return null;
    }
	
	public void addGamer(XOrules.Request req) {
		this.data.gamers.append(req.usr);
    }
	
	public void post_addGamer(XOrules.Request req) {
		this.sendRequest(req);
    }
	
	//#remove gamer
	public String pre_removeGamer(XOrules.Request req) {
		if (this.data.gamers.count(req.usr) == 0)
			return "no this gamer";
    }

	public void removeGamer(XOrules.Request req) {
		this.data.gamers.remove(req.usr);	
    }

	public void post_removeGamer(XOrules.Request req) {
		this.sendRequest(req);
    }

	//#setGamerParty
	public String pre_setGamerParty(XOrules.Request req) {
		if (this.data.gamers.count(req.usr) == 0)
			return "no this gamer";
		if (this.data.partyes.keys().count(req.body) == 0)
			return "no this party:" + req.body;
    }

	public void setGamerParty(XOrules.Request req) {
		this.data.partyes[req.body] = req.usr;
    }

	public void post_setGamerParty(XOrules.Request req) {
		this.sendRequest(req);
    }

	//#createAI
	public String pre_createAI(XOrules.Request req) {
    }

	public void createAI(XOrules.Request req) {
		//#public  _createAI():
		//#	import os; os.system('python XO\AIclient.py')
		//#import thread; thread.start_new_thread(_createAI, ())		
		import os; os.startfile("XO\AIclient.py");
    }

	public void post_createAI(XOrules.Request req){
    }

	//#start game
	public String pre_startGame(XOrules.Request req) {
		if (this.data.partyes[XOrules.Xchar] == null || this.data.partyes[XOrules.Ochar] == null)
			return "need two players";
        return null;
    }
	
	public void startGame(XOrules.Request req) {
		this.data.fld = XOrules.createField(this.data.size);
		this.data.who = XOrules.whoseTurn(this.data.fld);
		super.setState(GM_GAME);
    }
	
	public void post_startGame(XOrules.Request req) {
		this.sendRequest(req);
    }
	
	//#make turn
	public boolean _isItWin(this) {
		Character whoseWin_ = XOrules.whoseWin(this.data.fld, this.data.minChain);
		return !(whoseWin_ == XOrules.Fchar) &&  !(whoseWin_ == XOrules.NBchar);
    }

	public String pre_makeTurn(XOrules.Request req) {
    	if (this.data.partyes.get(this.data.who) != req.usr)
			return "it is not your turn";
		if (this._isItWin())
			return "the game is over";
		if (!XOrules.isItTurn(this.data.fld, this.data.who, req.body[0], req.body[1]))
			return "it is not avalibale turn";
        return null;
    }

	public void makeTurn(XOrules.Request req) {
    	XOrules.makeTurn(this.data.fld, this.data.who, req.body[0], req.body[1]);
		if (this._isItWin())
			this.stopGame();	
		else {
			if (this.data.who == XOrules.Xchar)
				this.data.who = XOrules.Ochar;
			else
				this.data.who = XOrules.Xchar;
        }
    }
	
	public void post_makeTurn(XOrules.Request req) {
		this.sendRequest(req);
    }
}
